package com.xhj.xmly.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.xhj.xmly.R;
import com.xhj.xmly.activity.ProvinceActivity;
import com.xhj.xmly.activity.RadioActivity;
import com.xhj.xmly.adapter.BroadcastAdapter;
import com.xhj.xmly.adapter.TextAdapter;
import com.xhj.xmly.bean.BroadcastEntity;
import com.xhj.xmly.bean.Radio;
import com.xhj.xmly.bean.RadioName;
import com.xhj.xmly.callback.BroadcastAdapterListener;
import com.xhj.xmly.callback.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Discover_broadcast_frag extends Fragment implements Callback<BroadcastEntity> ,BroadcastAdapterListener, View.OnClickListener {

    private List<Radio> list1;
    private List<Radio> list2;
    private BroadcastAdapter adapter1;
    private BroadcastAdapter adapter2;
    private RecyclerView recyclerView1;
    private RecyclerView recyclerView2;
    private GridView gridView;
    private List<String> datalist;
    private GridView girdview;
    private RecyclerView recyclerText;
    private TextAdapter textAdapter;
    private List<RadioName> radioNames;
    private int count = 0;
    private LinearLayout radioLinear;
    private Intent intent;

    public Discover_broadcast_frag() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_discover_broadcast_frag, container, false);
        recyclerView1 = (RecyclerView) view.findViewById(R.id.broadcast_recycler);
        recyclerView2 = (RecyclerView) view.findViewById(R.id.broadcast_recycler2);
        recyclerText = ((RecyclerView) view.findViewById(R.id.text_recycler));
        radioLinear = ((LinearLayout) view.findViewById(R.id.radio_linear));
        for (int i = 0; i < radioLinear.getChildCount(); i++) {
            radioLinear.getChildAt(i).setOnClickListener(this);
        }
        recyclerView1.setNestedScrollingEnabled(false);
        recyclerView2.setNestedScrollingEnabled(false);
        recyclerText.setNestedScrollingEnabled(false);
        init();
        return view;
    }
    private void init() {
        list1 = new ArrayList<Radio>();
        list2 = new ArrayList<Radio>();
        datalist = new ArrayList<>();
        adapter1 = new BroadcastAdapter(getContext(),list1);
        adapter2 = new BroadcastAdapter(getContext(),list2);
        recyclerView1.setAdapter(adapter1);
        recyclerView2.setAdapter(adapter2);
        textAdapter = new TextAdapter(getContext(),datalist,this);
        recyclerText.setLayoutManager(new GridLayoutManager(getContext(),4));
        recyclerText.setAdapter(textAdapter);


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpService service = retrofit.create(HttpService.class);
        service.getRadio().enqueue(this);
     }


    @Override
    public void onResponse(Call<BroadcastEntity> call, Response<BroadcastEntity> response) {
        List<Radio> localRadios = response.body().getData().getLocalRadios();
        List<Radio> topRadios = response.body().getData().getTopRadios();
        radioNames = response.body().getData().getCategories();
        for (int i = 0; i < 7; i++) {
            datalist.add(radioNames.get(i).getName());
        }
        datalist.add("");
        textAdapter.notifyDataSetChanged();
        list1.addAll(localRadios);
        list2.addAll(topRadios);
        adapter1.notifyDataSetChanged();
        adapter2.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<BroadcastEntity> call, Throwable t) {

    }

    @Override
    public void setCount() {
        count++;
        datalist.clear();
        int size = 0;
        if(count%2 == 1){
            size = radioNames.size();
        }else {
            size = 7;
        }
        for (int i = 0; i < size; i++) {
            datalist.add(radioNames.get(i).getName());
        }
        datalist.add("");
        textAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        String name = "";

        switch (v.getId()) {
            case R.id.radio_local:
                name = "本地台";
                break;
            case R.id.radio_country:
                name = "国家台";
                break;
            case R.id.radio_province:
                name = "省市台";
                break;
            case R.id.radio_internet:
                name = "网络台";
                break;
        }
        if(name.equals("省市台")){
            intent = new Intent(getContext(),ProvinceActivity.class);
        }else {
            intent = new Intent(getContext(), RadioActivity.class);
        }
        intent.putExtra("name",name);
        startActivity(intent);
    }
}
